<?php

class File_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $row
     * @return File
     */
    public static function getFileByRow($row)
    {
        $thatFile = new File;
        $thatFile->setId($row->id);
        $thatFile->setName($row->name);
        $thatFile->setHash($row->hash);
        $thatFile->setType($row->type);
        $thatFile->setSize($row->size);
        $thatFile->setUrl($row->url);
        $thatFile->setTime($row->time);
        $thatFile->setUserId($row->user_id);
        return $thatFile;
    }

    /**
     * @param File $thatFile
     * @return array
     */
    public static function getModelByFile($thatFile)
    {
        return array(
            'id'      => $thatFile->getId(),
            'name'    => $thatFile->getName(),
            'hash'    => $thatFile->getHash(),
            'type'    => $thatFile->getType(),
            'size'    => $thatFile->getSize(),
            'url'     => $thatFile->getUrl(),
            'time'    => $thatFile->getTime(),
            'user_id' => $thatFile->getUserId(),
        );
    }

    /**
     * @param int $id
     * @return File
     */
    public static function getFileById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('file.*');
        $CI->db->from('file');
        $CI->db->where('file.id', $id);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getFileByRow($row);
        else
            return null;
    }

    /**
     * @param string $hash
     * @return File
     */
    public static function getFileByHash($hash)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('file.*');
        $CI->db->from('file');
        $CI->db->where('file.hash', $hash);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getFileByRow($row);
        else
            return null;
    }

    /**
     * @param bool|int $limit
     * @param bool|int $offset
     * @return array
     */
    public static function getFileArray($limit = false, $offset = false)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('file.*');
        $CI->db->from('file');

        if ($limit !== false && is_numeric($limit)) {
            if ($offset === false || !is_numeric($offset))
                $offset = 0;
            $CI->db->limit($limit, $offset);
        }

        $query  = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getFileByRow($row);
        }
        return $array;
    }

    /**
     * @param File $thatFile
     * @throws DatabaseException
     * @return File
     */
    public static function add($thatFile)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $result = $CI->db->insert('file', self::getModelByFile($thatFile));
        if ($result)
            return self::getFileById($CI->db->insert_id());
        else
            throw new DatabaseException('插入数据失败');
    }

    /**
     * @param File $thatFile
     * @throws DatabaseException
     * @return File
     */
    public static function update($thatFile)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatFile->getID());
        $result = $CI->db->update('file', self::getModelByFile($thatFile));
        if ($result)
            return self::getFileById($thatFile->getID());
        else
            throw new DatabaseException('更新数据失败');
    }

    /**
     * @param File $thatFile
     * @throws DatabaseException
     * @return bool
     */
    public static function delete($thatFile)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatFile->getID());
        $result = $CI->db->delete('file');
        if ($result)
            return true;
        else
            throw new DatabaseException('删除数据失败');
    }

}
